﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sern.Abstracts.Tracker.DataContracts;
using Sern.Abstracts.Tracker.ServiceContracts;
using Sern.Abstracts.Tracker.ServiceContracts.Fakes;
using Sern.Common;
using Sern.Common.Fakes;
using Sern.ServiceImplementation.TrackerDefinition;
using Sern.ServiceImplementation.TrackerDefinition.Data;


namespace Sern.Test.ServiceImplementation.TrackerDefinition
{
    [TestClass]
    public class TrackerCommon_FiguresOutEndedJobs
    {
        [TestInitialize]
        public void Initialize()
        {
            ComponentLocator.Reset();

            Guid jobId = Guid.NewGuid();
            Guid[] clientIds = new[]
                {
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                    Guid.NewGuid()
                };

            Guid[] keyIds = new[]
                {    
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                };


            Guid[] redundancyIds = new[]
                {    
                    Guid.NewGuid(),
                    Guid.NewGuid(),
                    Guid.NewGuid()
                };



            ComponentLocator.Unity.RegisterInstance(
                    RepositoryFactory.CreateStub<Job>());

            ComponentLocator.Unity.Resolve<IRepository<Job>>()
                .Add(new JobDescription
                    {
                        Id = jobId,
                        StartTme = DateTime.UtcNow - TimeSpan.FromMinutes(5),
                        EndingTime = DateTime.UtcNow,
                    });

            ComponentLocator.Unity.RegisterInstance(
                    RepositoryFactory.CreateStub<Client>());

            ComponentLocator.Unity.Resolve<IRepository<Client>>()
                .AddMany(new []{
                    new ClientStatusInfo
                    {
                        JobId = jobId,
                        Id = clientIds[0],
                    },
                    new ClientStatusInfo
                    {
                        JobId = jobId,
                        Id = clientIds[1],
                    },
                    new ClientStatusInfo
                    {
                        JobId = jobId,
                        Id = clientIds[2],
                    },
                });
            
            ComponentLocator.Unity.RegisterInstance(
                RepositoryFactory.CreateStub<KeyAtClientLog>());

            ComponentLocator.Unity.Resolve<IRepository<KeyAtClientLog>>()
                .AddMany(new[]{
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[0],
                            RedundancyKey = redundancyIds[0],
                            State = (int) ProcessingStage.Initiated,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[0]
                        },
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[0],
                            RedundancyKey = redundancyIds[0],
                            State = (int) ProcessingStage.Processing,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[1]
                        },
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[0],
                            RedundancyKey = redundancyIds[0],
                            State = (int) ProcessingStage.Completed,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[2]
                        },
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[1],
                            RedundancyKey = redundancyIds[1],
                            State = (int) ProcessingStage.Initiated,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[0]

                        },
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[1],
                            RedundancyKey = redundancyIds[1],
                            State = (int) ProcessingStage.Processing,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[1]
                        },
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[1],
                            RedundancyKey = redundancyIds[1],
                            State = (int) ProcessingStage.Completed,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[2]
                        },
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[2],
                            RedundancyKey = redundancyIds[2],
                            State = (int) ProcessingStage.Initiated,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[0]
                        },
                    new KeyAtClientLog
                        {
                            Id = Guid.NewGuid(),
                            KeyId = keyIds[2],
                            RedundancyKey = redundancyIds[2],
                            State = (int) ProcessingStage.Processing,
                            TimeStamp = DateTime.UtcNow,
                            ClientId = clientIds[1]

                        },
                    //new KeyAtClientLog
                    //    {
                    //        Id = Guid.NewGuid(),
                    //        KeyId = keyIds[2],
                    //        RedundancyKey = redundancyIds[2],
                    //        State = (int) ProcessingStage.Completed
                    //    },
                });
            
        }

        [TestMethod]
        public void TestNotAllKeysAreCompleted()
        {
            Assert.IsTrue(!TrackerCommon.GetJobsAboutToEnd().Any());
        }
    }
}
